package mju.cxf.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mju.cxf.dto.CategoriesDTO;
import mju.cxf.dto.EmployeeDTO;
import mju.cxf.entity.Employee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.result.Result;
import mju.cxf.service.EmployeeService;
import mju.cxf.vo.EmployeeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员对员工操作相关接口
 */
@RestController
@Api(tags = "管理员对员工操作接口列表")
@RequestMapping("/admin/api/employee")
public class AdminEmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 新建员工信息
     *
     * @param employeeDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新建员工信息")
    public Result<String> createEmployee(@RequestBody EmployeeDTO employeeDTO) {
        return employeeService.saveEmployee(employeeDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 修改员工信息
     *
     * @param employeeDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改员工信息")
    public Result<String> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        return employeeService.updateEmployee(employeeDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 删除员工信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除员工信息", notes = "管理员通过此接口删除员工信息")
    public Result<String> deleteEmployee(@PathVariable Integer id) {
        return employeeService.deleteEmployee(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 批量删除分类信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    @ApiOperation(value = "批量删除员工信息", notes = "管理员通过此接口批量删除员工信息")
    public Result<String> deleteEmployees(@RequestBody List<Integer> ids) {
        return employeeService.deleteEmployees(ids) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 分页查询员工信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    public Page<EmployeeDTO> getUsersByName(@RequestParam(value = "name",required = false) String name,
                                            @RequestParam(value = "empNumber",required = false) String empNumber,
                                           @RequestParam(value = "current",defaultValue = "1") int current,
                                           @RequestParam(value = "size",defaultValue = "10") int size) {
        return employeeService.selectPageByName(empNumber, name, current, size);
    }
}
